<?php

namespace app\admin\controller;

use app\helper\Helper;
use think\Db;
use think\Controller;
use think\Exception;
use think\Request;
use think\Session;

class Base extends Controller
{
    protected $service;

    protected $model;

    public function __construct(Request $request = null)
    {
        header("Content-Type: text/html;charset=utf-8");
        // 指定允许其他域名访问
        header('Access-Control-Allow-Origin:*');
// 响应类型
        header('Access-Control-Allow-Methods:POST');
// 响应头设置
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        parent::__construct($request);
        //判断用户是否登陆
        $sessionData = Session::get('admin_info');
        if(!$sessionData){
            $this->redirect('Login/Login');
        }
    }

    /**
     * 获取全部资源
     * @return mixed
     */
    public function get()
    {
        return Helper::json($this->service->get(new $this->model));
    }

    /**
     * 获取一条资源
     * @param $id
     * @return mixed
     */
    public function find($id)
    {
        return  $this->service->find(new $this->model,$id);
    }

    /**
     * 新增资源
     * @param $input
     * @return mixed
     */
    public function store($input)
    {
        return $this->service->store(new $this->model,$input);
    }

    /**
     * 修改资源
     * @param $input
     * @param $id
     * @return mixed
     */
    public function put($input,$id)
    {
        return $this->service->put(new $this->model,$input,$id);
    }

    /**
     * 软删除一条资源
     * @param $id
     * @return mixed
     */
    public function soft_delete($id)
    {
        return $this->service->soft_delete(new $this->model,$id);
    }

    /**
     * 删除一条资源
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        return $this->service->delete(new $this->model,$id);
    }

    /**
     * 筛选方法
     * @param $arr
     * @param $post
     * @return array
     * 支持一维数组，和二维数组
     */
    public function select_param($arr,$post){
        if(!empty($arr)){
            $where=[];
            foreach ($arr as $key=>$value){
                if(is_array($value)){
                    $v=array_values($value);
                    $k=array_keys($value);
                    $a=explode('.',$v[0]);
                }else{
                    $a=explode('.',$value);
                }
                if (isset($post[$a[1]]) && $post[$a[1]]!=="") {
                    if($key=='like'){
                        $where[$value] = array('like', '%'.$post[$a[1]].'%');
                    }elseif($key=='array'){
                        $where[$v[0]]=[$k[0],$post[$a[1]]];
                    }else{
                        $where[$value] = $post[$a[1]];
                    }
                    $this->assign($a[1],$post[$a[1]]);
                }
            }
            return $where;
        }
    }
    protected function saveApiName($param,$apiName)
    {
        $str = implode(',', $param);
        $str = rtrim($str,',');
        $time = strtotime(date('Y-m-d',time()));
        $userId = 1;
        // $linkCount = Db::name('user_buy_api')
        //              // ->field('c.id,c.link_count')
        //              ->alias('c')
        //              ->join('')
        //              // ->where('user_id',$userId)
        //              // ->where('api_name',$apiName)
        //              ->find();
        // if($linkCount){
        //     $count = $linkCount['link_count'] + 1;
        //     Db::name('call_api')->where('id',$linkCount['id'])->update('link_count',$count);
        // }else{
        //     $data = [
        //         'user_id'=>$userId,
        //         'api_name'=>$apiName,
        //         'add_time'=>$time,
        //         'param'=>$str
        //     ];
        // }
    }

    /**
     * 获取用户信息
     * @return mixed
     */
    protected function getUserInfo()
    {
        $adminInfo = Session::get('admin_info');
        $userId = $adminInfo['id'];
        return $userId;
    }

    /**
     * 验证用户调用测试接口次数
     * @param $param
     * @return bool
     */
    protected function checkTestApiCount($param)
    {
        $time = strtotime(date('Y-m-d',time()));
        $userId = $this->getUserInfo();
        $apiId = $param['urltype'];
        $count = Db::name('api_test_log')->where('user_id',$userId)->where('api_id',$apiId)->where('add_time',$time)->count();
        if($count >= 5){
            echo json_encode(array('code'=>40004,'msg'=>'当天测试次数使用完毕!'));
            return false;
        }
    }

    /**
     * 调用测试接口成功时候记录调用
     * @param $apiId
     */
    protected function addApplyTestApiCount($apiId)
    {
        $data = [
            'user_id'=>$this->getUserInfo(),
            'add_time'=>strtotime(date('Y-m-d',time())),
            'api_id'=>$apiId
        ];
        Db::name('api_test_log')->insert($data);
    }
}